package com.intct.util;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import java.io.IOException;

/**
 * @author gufg
 * @since 2025-07-27 15:04
 */
public class HBaseUtil {

    private static Connection connection = null;

    static {
        try {
            connection = ConnectionFactory.createConnection();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 创建HBase表的方法
     * @param namespace 命名空间名称
     * @param tName     表名称
     * @param family    列族名称
     * @throws IOException
     */
    public static void createTable(String namespace, String tName, String family) throws IOException {
        // 创建admin
        Admin admin = connection.getAdmin();

        TableName tableName = TableName.valueOf(namespace, tName);

        if (!admin.tableExists(tableName)) {
            // 创建表
            try {
                TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);

                ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(family.getBytes());
                columnFamilyDescriptorBuilder.setMaxVersions(3);

                tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());

                admin.createTable(tableDescriptorBuilder.build());
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                if (admin != null) {
                    // 关闭
                    admin.close();
                }
            }
        }
    }

    /**
     * 向HBase插入数据
     * @param namespace 命名空间名称
     * @param tName     表名称
     * @param put       Put实例
     * @throws IOException
     */
    public static void put(String namespace, String tName, Put put) throws IOException {
        // 创建admin
        Admin admin = connection.getAdmin();

        TableName tableName = TableName.valueOf(namespace, tName);

        // 创建table
        Table table = connection.getTable(tableName);

        // 判断表名称已经存
        if (admin.tableExists(tableName)) {
            // 保存数据
            try {
                table.put(put);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                if (admin != null) {
                    // 关闭
                    admin.close();
                }
                if (table != null) {
                    table.close();
                }
            }
        }
    }

    /**
     * 根据rowkey查询数据
     * @param namespace
     * @param tName
     * @param rowkey
     * @return
     * @throws IOException
     */
    public static Result getRowkey(String namespace, String tName, String rowkey) throws IOException {
        // 创建admin
        Admin admin = connection.getAdmin();

        TableName tableName = TableName.valueOf(namespace, tName);

        // 创建table
        Table table = connection.getTable(tableName);

        // 判断表名称已经存
        if (admin.tableExists(tableName)) {
            // 保存数据
            try {
                Get get = new Get(rowkey.getBytes());
                return table.get(get);

            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                if (admin != null) {
                    // 关闭
                    admin.close();
                }
                if (table != null) {
                    table.close();
                }
            }
        }
        return null;
    }
}
